//
//  DDMainMusicViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/8.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDMainMusicViewController.h"
#import "DDNetWorkTools.h"
#import "MyScrollView.h"
#import <Masonry.h>
#import "UILabel+DDUILabelCategory.h"
#import "DDMainMusicCell.h"
#import "DDMainMusicCellViewModel.h"
#import <AVObject.h>
#import "DDMeViewController.h"
#import "DDMusicClassViewController.h"
#import "DDPlayMusicViewController.h"
#import "DDPublicMacro.h"

#define CELL_IDEFIFIER @"DDMainMusicCell"
@interface DDMainMusicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIImageView* bgImageView;//背景图片
@property (nonatomic,strong)MyScrollView* scrollView;//滚动视图
@property (nonatomic,strong)UITableView* tableView;//表格视图

@property (nonatomic,strong)DDMainMusicCellViewModel* viewModel;//视图模型

@end

@implementation DDMainMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self layoutUI];
    [self registerNotificationCenter];
    self.viewModel = [[DDMainMusicCellViewModel alloc]init];
  __weak  DDMainMusicViewController* weakSelf = self;
    [self.viewModel loadDataWithStyle:HotMusicDataStyle isOK:^(BOOL isOk, NSError *error) {
        if (isOk) {
            [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:(UITableViewRowAnimationAutomatic)];
         
           
        }
    [self.viewModel loadDataWithStyle:RecommendMusicDataStyle isOK:^(BOOL isOk, NSError *error) {
        if (isOk) {
    [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:(UITableViewRowAnimationAutomatic)];
           
        }
    }];
    }];
}
- (void)viewDidAppear:(BOOL)animated{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //启动滚动视图
        self.scrollView.imageNames = @[@"scrollView5",@"scrollView1",@"scrollView2",@"scrollView3",@"scrollView4",@"scrollView5",@"scrollView1"];
    });

    
}
#pragma mark 布局UI
- (void)layoutUI{
//bgImageView
    self.bgImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    self.bgImageView.image = [UIImage imageNamed:@"mainBgImageView"];
    [self.view addSubview:self.bgImageView];
   

//设置搜索按钮
    UIButton* searchBtn = [[UIButton alloc]init];
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"search"] forState:(UIControlStateNormal)];
    searchBtn.frame = CGRectMake(0, 0, 30, 30);
    [searchBtn addTarget:self action:@selector(searchBtnClicekd) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem* searchItem = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = searchItem;
    
//tableView
    NSInteger tableViewY = 0;
    self.tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, tableViewY, SCREEN_SIZE.width, SCREEN_SIZE.height) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 200;
     self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerClass:[DDMainMusicCell class] forCellReuseIdentifier:CELL_IDEFIFIER];
     [self.view addSubview:self.tableView];
//tableView头视图
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 0.35*SCREEN_SIZE.height)];
    headView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = headView;

//scrollView
    self.scrollView = [[MyScrollView alloc]init];
    [headView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@(0.25*SCREEN_SIZE.height));
    }];
  
    NSArray* btnImagesArr = @[@"classify",@"kMusic",@"dynamic-1",@"me"];
    NSArray* btnTitileArr = @[@"歌曲分类",@"k歌",@"动态",@"我的"];
//四个点击按钮
    NSInteger btnHeight = 0.1*SCREEN_SIZE.height - 30;//按钮的宽高
    NSInteger btnPitch = (SCREEN_SIZE.width-btnHeight*4)/5;//按钮之间的间距
    for (NSInteger i = 0; i < 4; i++) {
        UIButton* btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
     
        [btn setBackgroundImage:[UIImage imageNamed:btnImagesArr[i]] forState:(UIControlStateNormal)];
        btn.tag = 200+i;
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
        [headView addSubview:btn];
        UILabel* label = [UILabel dd_CreatLabelWithTitle:btnTitileArr[i] font:16 TitleColor:[UIColor blackColor] TextAlignment:(NSTextAlignmentCenter)];
        [headView addSubview:label];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(btnHeight));
            make.top.equalTo(@(0.25*SCREEN_SIZE.height+8));
            make.left.equalTo(@(btnPitch+(btnHeight+btnPitch)*i));
           
            
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(4);
            make.centerX.equalTo(btn.mas_centerX);
        }];
    }
   
    
}



#pragma mark 逻辑操作
#pragma mark 搜索按钮的回调方法
- (void)searchBtnClicekd{
  /* AVObject *todo =[AVObject objectWithClassName:@"RecommendMusic" objectId:@"5823bca72e958a12992399b3"];
    // 修改属性
    [todo setObject:@[@{@"rankNumber":@"1",@"albumpic_big":@"http://i0.hdslb.com/video/6c/6c6b31fb3870b74183666f05f0bc7c41.jpg",@"url":@"http://sc1.111ttt.com/2015/1/12/01/105011606509.mp3",@"singername":@"王真",@"songname":@"雨梦",@"seconds":@"164",@"downUrl":@"",@"lrc":@""},@{@"rankNumber":@"2",@"albumpic_big":@"http://upload.mcchina.com/2016/0118/1453102189288.JPG",@"url":@"http://sc1.111ttt.com/2014/1/12/19/5191950035.mp3",@"singername":@"徐佳莹",@"songname":@"身骑白马",@"seconds":@"313",@"downUrl":@"",@"lrc":@""},@{@"rankNumber":@"3",@"albumpic_big":@"http://img.sgamer.com/lol_sgamer_com/images/20140918/94ccdca4589cfd9fcf088b0fa869b872.png",@"url":@"http://sc1.111ttt.com/2015/5/08/05/101051156139.mp3",@"singername":@"梦龙",@"songname":@"Warriors",@"seconds":@"169",@"downUrl":@"",@"lrc":@""},@{@"rankNumber":@"4",@"albumpic_big":@"http://zx.youdao.com/zx/wp-content/uploads/2014/01/one-republic-pic.jpg",@"url":@"http://sc1.111ttt.com/2015/1/03/13/96131910159.mp3",@"singername":@"OneReppublic",@"songname":@"Counting Stars",@"seconds":@"267",@"downUrl":@"",@"lrc":@""}]forKey:@"muscis"];
    // 保存到云端
    [todo saveInBackground];*/
    
 
    
    
    
}
#pragma mark 四个按钮的回调方法
- (void)btnClicked:(UIButton*)button{
 
    switch (button.tag) {
        case 200:{
            DDMusicClassViewController* musicClassVC = [DDMusicClassViewController new];
            musicClassVC.hidesBottomBarWhenPushed = YES;
            musicClassVC.title = @"音乐广场";
            [self.navigationController pushViewController:musicClassVC animated:YES];
        }
           
          break;
        case 201:
            //k歌
            break;
        case 202:
            //动态
            break;
        case 203:{
            
            //我的
            DDMeViewController* meVC = [[DDMeViewController alloc]init];
            meVC.hidesBottomBarWhenPushed = YES;
            meVC.title = @"我的";
            [self.navigationController pushViewController:meVC animated:YES];
        }
            break;
        default:
            break;
    }

}
#pragma mark 表格视图的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DDMainMusicCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDEFIFIER forIndexPath:indexPath];
    if (indexPath.row ==0) {
        cell.style = HotMusicStyle;
        cell.viewModel = self.viewModel;
    }else if (indexPath.row ==1){
        cell.style = RecommendMusicStyle;
        cell.viewModel = self.viewModel;
        
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 10)];
    headView.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    return headView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
#pragma mark 通知相关的
#pragma mark 注册通知
- (void)registerNotificationCenter{
//标签栏播放按钮被点击后的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(toPlayMusicVc:) name:@"DDMainTabBarPlayingMusicBtnClicked" object:nil];
//推荐排行cell歌曲点击后的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(toPlayMusicVc:) name:@"DDMainRankTableViewCellClicked" object:nil];

}
#pragma mark 通知的回调方法
- (void)toPlayMusicVc:(NSNotification*)notification{
    NSDictionary* dic = notification.object;
    DDPlayMusicViewController* playMusicVC = [DDPlayMusicViewController sharedDDPlayMusicViewController];
    playMusicVC.hidesBottomBarWhenPushed = YES;
    [self presentViewController:playMusicVC animated:YES completion:nil];
    if ([dic[ISPLAYNEWMUSIC] isEqualToString:@"YES"]) {
        playMusicVC.playMusicList = dic[NEWPLAYMUSICLIST];
    }
    
}

@end
